package example.ilm.utils;

import example.ilm.model.GeometricElement;
import example.ilm.model.Polygon;
import example.ilm.model.Workspace;
import java.awt.geom.Point2D;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.List;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

/**
 *
 * @author Tulio Faria <tuliofaria@gmail.com>
 */
public class I2g {

    public static void createFile(String savePath, List currentState) {
        // saving screenshot
        
        
        String baseDir = "10"; //System.currentTimeMillis()+"";
        String tempPath = baseDir + "/construction/";
        Screenshot.takeScreenShot(tempPath+"", "preview.png");
        new File(tempPath).mkdirs();
        new File(tempPath + "intergeo.xml").delete();
        int count = 0;
        String constraints = "<polygon_by_vertices>\n";
        try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tempPath + "intergeo.xml", true)));
            out.println("<construction xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"../xml/intergeo.xsd\">");
            out.println("<elements>");
            int i, t = currentState.size();
            for (i = 0; i < t; i++) {
                GeometricElement ge = (GeometricElement) currentState.get(i);
                if (ge instanceof Polygon) {
                    // tratando polygon
                    Polygon p = (Polygon) ge;
                    List vertices = p.getVertices();
                    
                    int k = 0, tK = vertices.size();
                    constraints+="" +
"			<polygon>p"+count+"</polygon><list_of_vertices>";
                    for (k = 0; k < tK; k++) {
                        constraints+="<point>A"+count+"</point>";
                        
                        out.println("<point id=\"A"+(count++)+"\">");
                        out.println("<euclidean_coordinates>");
                        Point2D p2d = (Point2D) vertices.get(k);
                        out.println("<double>");
                        out.println("" + ((p2d.getX()+ge.getPosition().getX()))/40);
                        out.println("</double>");
                        out.println("<double>");
                        out.println("" + ((p2d.getY()+ge.getPosition().getY()))/40);
                        out.println("</double>");
                        out.println("</euclidean_coordinates>");
                        out.println("</point>");
                    }
                    constraints+="</list_of_vertices>\n";
                }
            }
            constraints+="</polygon_by_vertices>";
            out.println("</elements>");
            out.println("<constraints>\n"
                    + constraints
                    + "</constraints>\n"
                    
                    + "</construction>");
            out.close();
        } catch (Exception e) {
            //oh noes!
        }
        
        try {
			// Initiate ZipFile object with the path/name of the zip file.
			ZipFile zipFile = new ZipFile("package.i2g");
			
			// Folder to add
			String folderToAdd = "10/construction";
			
			// Initiate Zip Parameters which define various properties such
			// as compression method, etc.
			ZipParameters parameters = new ZipParameters();
			
			// set compression method to store compression
			parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
			
			// Set the compression level
			parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
			
                        
                        
			// Add folder to the zip file
			zipFile.addFolder(folderToAdd, parameters);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    
}
